 var Drawgrid = require('./drawgrid');
 var fs = require('fs');
 var path = require('path');
 var escposjs="./escpos/index"+".js";
 var escpos = require(escposjs);

 var mslibrary = require('ms-common-library');
 var ArFor = mslibrary.ArFor;
 var FontList = {}
 var jsonPrinter = function (PrinterDB, Callback) {
 	var inithtml = function () {
 		if (document.getElementById('ms-printer') == null) {
 			var canvas = document.createElement("canvas");
 			canvas.id = "ms-printer";
 			///canvas.style.visibility = "hidden";
 			document.body.append(canvas);
 		}
 	}
 	var addFonts = function (obj) {
 		var style = document.createElement("style");
 		style.type = "text/css";
 		var off = false;
 		for (var key in obj) {
 			var url = obj[key];
 			if (FontList[key] == null) {
 				FontList[key] = true;
 				var val = "@font-face {font-family: '" + key + "';src:url('" + url + "') format('opentype');}"
 				try {
 					style.appendChild(document.createTextNode(val));
 				} catch (ex) {
 					style.styleSheet.cssText = val
 				}
 				off = true;
 			}
 		}
 		var head = document.getElementsByTagName("head")[0];
 		if (off) {
 			head.appendChild(style);
 		}
 		return off;
 	}
 	var msPrinter = function (obj, Callback) {

 		/*
 		var OriginalFile = obj.src;
 		var file = path.join(__dirname, "ms.png");
 		var base64Data = OriginalFile.replace(/^data:image\/png;base64,/, "")
 		fs.writeFile(file, base64Data, 'base64', function (err) {
 			Callback(err)
 		})
 		Callback()
 		return;
 		var device = new escpos.Console();
 		*/

 		var Network = obj.Network;

 		var  device = new escpos.Network(Network.address, Network.port);

 		var  printer = new escpos.Printer(device);

 		//var buffer = qr.imageSync("nihao", { type: 'png', mode: 'dhdw' });

 		escpos.Image.load(obj.src, function (image) {
 			device.open(function (error) {
 				if (error) {
 					Callback(error);
 					return
 				}
 				printer.align('ct');
 				printer.image(image, 's8');
 				printer.image(image, 'd8');
 				printer.image(image, 's24');
 				printer.image(image, 'd24');
 				printer.raster(image);
 				printer.raster(image, 'dw');
 				printer.raster(image, 'dh');
 				printer.raster(image, 'dwdh');
 				printer.cut()
 				printer.close(function () {
 					Callback(null, {});
 				});
 			});
 		});
 	}
 	var off = addFonts(PrinterDB.Fonts);
 	var Fontls = [];
 	if (off) {
 		Fontls = [1]
 	}

 	inithtml();

 	var gridform = document.getElementById('ms-printer');

 	new ArFor(Fontls, function () {
 		Drawgrid(gridform, PrinterDB, function (src) {
 			var PrinterImage = function () {
 				PrinterDB.src = src;
 				new msPrinter(PrinterDB, function (error) {
 					Callback(error)
 				})
 			};
 			if (typeof PrinterDB.preview == 'function') {
 				PrinterDB.preview(src, function () {

 					PrinterImage();

 				})
 			} else {
 				PrinterImage()
 			}

 		});
 	}).forEach(function (item, next) {
 		Drawgrid(gridform, PrinterDB, function (src) {
 			next()
 		})
 	})

 }
 window.onPrinter = function (PrinterDB, fs) {
 	new jsonPrinter(PrinterDB, function (error) {
 		window.close(true);
 	})
 }